<?php
declare (strict_types = 1);

namespace app\controller\admin;

use app\model\AdminAppRotation;
use app\model\AdminTeachingBuilding;
use app\util\ReturnCode;
use think\facade\Request;
use think\facade\Validate;
use think\Response;

class AppRotation extends Base {

    /**
     * 显示APP轮播图列表
     *
     * @return \think\Response
     */
    public function index(): Response
    {
        $limit = $this->request->get('size', config('apiadmin.ADMIN_LIST_DEFAULT'));
        $start = $this->request->get('page', 1);
        $school_id = $this->request->get('school_id', $this->userInfo['school_id']);
        $sortKey = $this->request->get('sortKey', 'create_time');
        $order = $this->request->get('order', 'DESC');

        $where = [];
        $obj = (new AdminAppRotation());
        if ($school_id > 0) {
            array_push($where, ['school_id', '=', $school_id]);
        }

        $listObj = $obj->where($where)
            ->with(['school'])
            ->order($sortKey, $order)
            ->paginate(['page' => $start, 'list_rows' => $limit])->toArray();

        return $this->buildSuccess([
            'list'  => $listObj['data'],
            'count' => $listObj['total']
        ]);
    }

    /**
     * 保存APP轮播图
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function add(): Response
    {
        $data = Request::only(['title','school_id','images'], 'post');
        $validate = Validate::rule([
            'title|标题'=>'require',
            'images|图片'=>'require'
        ]);
        if (!$validate->check($data)) {
            return $this->buildFailed(ReturnCode::PARAM_INVALID, $validate->getError());
        }

        $res = AdminAppRotation::create([
            'school_id' => $this->request->post('school_id'),
            'title'  => $this->request->post('title', ''),
            'images' => $this->request->post('images', '')
        ]);
        if ($res === false) {
            return $this->buildFailed(ReturnCode::DB_SAVE_ERROR);
        }

        return $this->buildSuccess();
    }


    /**
     * 编辑APP轮播图
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit(): Response
    {
        $data = Request::only(['title','school_id','images'], 'post');
        $validate = Validate::rule([
            'title|标题'=>'require',
            'images|图片'=>'require'
        ]);
        if (!$validate->check($data)) {
            return $this->buildFailed(ReturnCode::PARAM_INVALID, $validate->getError());
        }

        $res = AdminAppRotation::update([
            'id'        => $this->request->post('id', 0),
            'school_id' => $this->request->post('school_id'),
            'title'     => $this->request->post('title', ''),
            'images'    => $this->request->post('images', '')
        ]);
        if ($res === false) {
            return $this->buildFailed(ReturnCode::DB_SAVE_ERROR);
        }

        return $this->buildSuccess();
    }


    /**
     * 删除指定APP轮播图
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function del(): Response
    {
        $id = $this->request->get('id');
        if (!$id) {
            return $this->buildFailed(ReturnCode::EMPTY_PARAMS, '缺少必要参数');
        }
        AdminAppRotation::destroy($id);

        return $this->buildSuccess();
    }

}
